//
//  SignUpViewController.swift
//  Signup
//
//  Created by heze on 2018/9/29.
//  Copyright © 2018 apple. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class SignUpViewController: UIViewController {

    var disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        view.addSubview(usernameTextField)
        view.addSubview(usernameLabel)
        view.addSubview(passwordTextField)
        view.addSubview(passwordLabel)
        view.addSubview(repeatedPasswordTextField)
        view.addSubview(repeatedPasswordLabel)
        view.addSubview(signupOutButton)
        signupOutButton.addSubview(activityView)
        
        setup()
        
    }
    
    private func setup() {
        
        let viewModel = GithubGignupViewModel(input: (
            username: usernameTextField.rx.text.orEmpty.asObservable(),
            password: passwordTextField.rx.text.orEmpty.asObservable(),
            repeatedPassword: repeatedPasswordTextField.rx.text.orEmpty.asObservable(),
            loginTaps: signupOutButton.rx.tap.asObservable()
            ), dependency: (
                API: GitHubDefaultAPI.sharedAPI,
                validationService: GitHubDefaultValidationService.sharedValidationService,
                wireframe: DefaultWireframe.shared))
        
        viewModel.signupEnabled
            .subscribe(onNext: { [weak self](valid) in
            
            self?.signupOutButton.isEnabled = valid
            self?.signupOutButton.alpha = valid ? 1.0 : 0.5
            
        })
            .disposed(by: disposeBag)
        
        viewModel.validateUsername
            .bind(to: usernameLabel.rx.validationResult)
            .disposed(by: disposeBag)
        
        viewModel.validatePassword
            .bind(to: passwordLabel.rx.validationResult)
            .disposed(by: disposeBag)
        
        viewModel.validatePasswordRepeated
            .bind(to: repeatedPasswordLabel.rx.validationResult)
            .disposed(by: disposeBag)
        
        viewModel.signingIn
            .bind(to: activityView.rx.isAnimating)
            .disposed(by: disposeBag)
        
        viewModel.signedIn
            .subscribe(onNext: { signedIn in
            print("User signed in \(signedIn)")
        })
            .disposed(by: disposeBag)
        
        let tapBackground = UITapGestureRecognizer()
        view.addGestureRecognizer(tapBackground)
        
        tapBackground.rx.event.subscribe(onNext: { [weak self](_) in
            
            self?.view.endEditing(true)
            
        }).disposed(by: disposeBag)
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        usernameTextField.ly
            .set_size(size: CGSize(width: self.view.ly.width - 32, height: 30))
            .set_left(left: 16)
            .set_top(top: 100)
            .end()
        
        usernameLabel.ly
            .set_size(size: CGSize(width: self.view.ly.width - 32, height: 20))
            .leftEqual(usernameTextField)
            .set_top(top: usernameTextField.ly.bottom, offset: 10)
            .end()
        
        passwordTextField.ly
            .sizeEqual(usernameTextField)
            .leftEqual(usernameTextField)
            .set_top(top: usernameLabel.ly.bottom, offset: 10)
            .end()
        
        passwordLabel.ly
            .sizeEqual(usernameLabel)
            .leftEqual(passwordTextField)
            .set_top(top: passwordTextField.ly.bottom, offset: 10)
            .end()
        
        repeatedPasswordTextField.ly
            .sizeEqual(passwordTextField)
            .leftEqual(passwordLabel)
            .set_top(top: passwordLabel.ly.bottom, offset: 10)
            .end()
        
        repeatedPasswordLabel.ly
            .sizeEqual(passwordLabel)
            .leftEqual(repeatedPasswordTextField)
            .set_top(top: repeatedPasswordTextField.ly.bottom, offset: 10)
            .end()
        
        signupOutButton.ly
            .set_size(size: CGSize(width: self.view.ly.width - 32, height: 50))
            .leftEqual(repeatedPasswordLabel)
            .set_top(top: repeatedPasswordLabel.ly.bottom, offset: 20)
            .end()
        
        activityView.ly
            .sizeToFit()
            .set_centerY(centerY: signupOutButton.ly.middleY)
            .set_left(left: 10)
            .end()
        
    }
    
    //MARK: getter and setter
    fileprivate lazy var usernameTextField: UITextField = {
        
        let usernameTextField = UITextField()
        usernameTextField.font = UIFont.systemFont(ofSize: 14.0)
        usernameTextField.addCutomStyle()
        
        return usernameTextField
    }()
    
    fileprivate lazy var usernameLabel: UILabel = {
        
        let usernameLabel = UILabel()
        usernameLabel.textColor = UIColor.red
        usernameLabel.font = UIFont.systemFont(ofSize: 14.0)
        return usernameLabel
    }()
    
    fileprivate lazy var passwordTextField: UITextField = {
        
        let passwordTextField = UITextField()
        passwordTextField.font = UIFont.systemFont(ofSize: 14.0)
        passwordTextField.keyboardType = .asciiCapable
        passwordTextField.addCutomStyle()
        
        return passwordTextField
    }()
    
    fileprivate lazy var passwordLabel: UILabel = {
        
        let passwordLabel = UILabel()
        passwordLabel.textColor = UIColor.red
        passwordLabel.font = UIFont.systemFont(ofSize: 14.0)
        
        return passwordLabel
    }()
    
    fileprivate lazy var repeatedPasswordTextField: UITextField = {
        
        let repeatedPasswordTextField = UITextField()
        repeatedPasswordTextField.font = UIFont.systemFont(ofSize: 14.0)
        repeatedPasswordTextField.keyboardType = .asciiCapable
        repeatedPasswordTextField.addCutomStyle()
        
        return repeatedPasswordTextField
    }()
    
    fileprivate lazy var repeatedPasswordLabel: UILabel = {
        
        let repeatedPasswordLabel = UILabel()
        repeatedPasswordLabel.textColor = UIColor.red
        repeatedPasswordLabel.font = UIFont.systemFont(ofSize: 14.0)
        return repeatedPasswordLabel
    }()
    
    fileprivate lazy var signupOutButton: UIButton = {
        
        let signupOutButton = UIButton()
        signupOutButton.backgroundColor = UIColor.green
        signupOutButton.setTitleColor(UIColor.white, for: .normal)
        signupOutButton.setTitle("sign up", for: .normal)
        signupOutButton.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
        signupOutButton.layer.masksToBounds = true
        signupOutButton.layer.cornerRadius = 5.0
        
        return signupOutButton
    }()
    
    fileprivate lazy var activityView: UIActivityIndicatorView = {
        
        return UIActivityIndicatorView()
    }()
}


private extension UITextField {
    
    func addCutomStyle() {
        
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 3.0
        self.backgroundColor = UIColor.white
        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.borderWidth = 1.0
        
    }
}
